package com.yanqu.road.server.logic.season;

import com.yanqu.road.entity.activity.washe.config.WasheConfig;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.season.SeasonRewardConfig;
import com.yanqu.road.entity.season.SeasonRewardData;
import com.yanqu.road.entity.enums.eSeasonRewardType;
import com.yanqu.road.entity.season.SeasonRewardKey;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.season.SeasonRewardModule;
import com.yanqu.road.server.logic.season.args.ReceiveSeasonRewardResult;
import com.yanqu.road.server.manger.season.SeasonRewardMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public abstract class BaseSeasonReward {

    public static final Logger logger = LogManager.getLogger(BaseSeasonReward.class.getName());

    protected GamePlayer player;
    protected eSeasonRewardType type;
    protected int systemId;
    protected int seasonId;

    public BaseSeasonReward(eSeasonRewardType type, GamePlayer player) {
        this.player = player;
        this.type = type;
        this.systemId = initSystemId();
        this.seasonId = initSeasonId();
    }

    // ------------------------------------------------

    public void handle(Object param) {

        Set<Integer> seasonIdSet = new HashSet<>();
        seasonIdSet.add(0); // 常驻任务，只能完成一次的，配置成0赛季
        seasonIdSet.add(seasonId);

        SeasonRewardModule seasonRewardModule = player.getModule(SeasonRewardModule.class);
        for (int seasonId : seasonIdSet) {
            Map<Integer, SeasonRewardConfig> configMap = SeasonRewardMgr.getSeasonRewardConfigMap(systemId, seasonId, type.getIntValue());
            if (configMap.isEmpty()) {
                continue;
            }
            Map<Integer, SeasonRewardData> dataMap = seasonRewardModule.getSeasonRewardDataMap(systemId, seasonId, type);
            if (dataMap.isEmpty()) {
                continue;
            }
            Map<Integer, SeasonRewardData> changeMap = new HashMap<>();
            for (SeasonRewardConfig config : configMap.values()) {
                SeasonRewardData data = dataMap.get(config.getRewardId());
                if (data != null && !data.isGetReward() && canChange(config, data, param)) {
                    changeValue(config, data, param);
                    data.setTime(System.currentTimeMillis());
                    changeMap.put(data.getRewardId(), data);
                }
            }
            seasonRewardModule.addChangeData(changeMap);
        }
    }

    public ReceiveSeasonRewardResult receiveReward(SeasonRewardKey key, List<Integer> idList) {

        // 只能领取0赛季或者当前赛季的奖励
        if (key.getSeasonId() != WasheConfig.SEASON_ID_NULL && key.getSeasonId() != seasonId) {
            return new ReceiveSeasonRewardResult(GameErrorCode.E_SEASON_REWARD_SEASON_NO_EXIST);
        }

        // 获取数据
        SeasonRewardModule seasonRewardModule = player.getModule(SeasonRewardModule.class);
        Map<Integer, SeasonRewardData> dataMap = seasonRewardModule.getSeasonRewardDataMap(systemId, key.getSeasonId(), type);

        // 设置领奖状态
        Property property = new Property();
        Map<Integer, SeasonRewardData> changeMap = new HashMap<>();
        for (int id : idList) {
            SeasonRewardData data = dataMap.get(id);
            if (data == null) {
                continue;
            }
            SeasonRewardConfig config = SeasonRewardMgr.getSeasonRewardConfig(data);
            if (config == null) {
                logger.error("can not find SeasonRewardConfig systemId {} seasonId {} type {} rewardId {}",
                        data.getSystemId(), data.getSeasonId(), data.getType(), data.getRewardId());
                continue;
            }
            if (!data.isGetReward() && isComplete(config, data)) {
                data.setGetReward(true);
                data.setTime(System.currentTimeMillis());
                property.addProperty(PropertyHelper.parseStringToProperty(config.getReward()));
                changeMap.put(data.getRewardId(), data);
                // 自定义日志
                addLog(config, data);
            }
        }
        seasonRewardModule.addChangeData(changeMap);

        // 看看有没有奖励
        if (changeMap.isEmpty() || property.isNothing()) {
            return new ReceiveSeasonRewardResult(GameErrorCode.E_SEASON_REWARD_NO_COMPLETE);
        }

        // 加奖励
        player.getModule(CurrencyModule.class).addCurrency(property, eLogMoneyType.Season, eLogMoneyType.SeasonGetReward);

        return new ReceiveSeasonRewardResult(PropertyHelper.parsePropertyToString(property));
    }

    // ------------------------------------------------

    public int getSystemId() {
        return systemId;
    }

    public int getSeasonId() {
        return seasonId;
    }

    public eSeasonRewardType getType() {
        return type;
    }

    public GamePlayer getPlayer() {
        return player;
    }

    protected abstract int initSystemId();

    protected abstract int initSeasonId();

    protected void init() {
        // 构造方法后调用，根据需要Override
    }

    protected abstract boolean canChange(SeasonRewardConfig config, SeasonRewardData data, Object param);

    protected abstract void changeValue(SeasonRewardConfig config, SeasonRewardData data, Object param);

    protected abstract boolean isComplete(SeasonRewardConfig config, SeasonRewardData data);

    protected void addLog(SeasonRewardConfig config, SeasonRewardData data) {
        // 设置领奖状态后调用
    }
}
